package com.feng.spring.hibernate.extend.proxy;


import com.feng.spring.hibernate.extend.HibernateExtendFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * FactoryBean
 *
 * @author 张俊峰
 */
public class HibernateExtendFactoryBean<T> implements FactoryBean<T> {

    public HibernateExtendFactoryBean(Class<T> hibernateInterface) {
        this.hibernateInterface = hibernateInterface;
    }

    private Class<T> hibernateInterface;
    private HibernateExtendFactory hibernateExtendFactory;

    @SuppressWarnings("unchecked")
    @Override
    public T getObject() throws Exception {
        return (T) new HibernateExtendProxyFactory(hibernateInterface).newInstance(getHibernateExtendFactory());
    }

    @Override
    public Class<?> getObjectType() {
        return hibernateInterface;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public HibernateExtendFactory getHibernateExtendFactory() {
        return hibernateExtendFactory;
    }

    @Autowired
    public void setHibernateExtendFactory(HibernateExtendFactory hibernateExtendFactory) {
        this.hibernateExtendFactory = hibernateExtendFactory;
    }
}
